package com.iterable.demo1;

import java.util.Iterator;

//创建的任何实现了Iterable的类，都可以用于foreach语句
public class IterableClass implements Iterable<String> {

	private String[] words = "my name is zhang su lei".split(" ");

	@Override
	public Iterator<String> iterator() {
		return new Iterator<String>() {

			private int index = 0;

			@Override
			public boolean hasNext() {
				return index < words.length;
			}

			@Override
			public String next() {
				return words[index++];
			}

			@Override
			public void remove() {
				throw new UnsupportedOperationException();
			}

		};
	}

	public static void main(String[] args) {
		for (String string : new IterableClass()) {
			System.out.println(string);
		}
	}

}
